package com.zhitong.money.mapper;

import com.zhitong.money.entity.CheckTime;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface CheckMapper {

    @Results(id = "checkMap",value = {
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "tid",property = "tid"),
            @Result(column = "date",property = "date"),
            @Result(column = "startTime",property = "startTime"),
            @Result(column = "endTime",property = "endTime"),
            @Result(column = "status",property = "status"),
            @Result(column = "result",property = "result"),
    })
    @Select("select * from checkTime")
    public List<CheckTime> findAllCheck();


    @ResultMap("checkMap")
    @Select("select checkTime.*,employee.name from checkTime,employee where checkTime.tid = employee.tellerId and checkTime.tid=#{tid}")
    public List<CheckTime> findCheckByTid(String tid);


    @ResultMap("checkMap")
    @Select({
            "<script>",
            "select checkTime.*,employee.name from checkTime,employee",
            "<where>",
            "checkTime.tid=employee.tellerId",
            "   <if test='tid!=null and tid!=\"\"'>and checkTime.tid=#{tid}</if>",
            "   <if test='name!=null and name!=\"\"'>and employee.name like concat(concat('%',#{name},'%'))</if>",
            "   <if test='date!=null and date!=\"\"'>and checkTime.date=#{date}</if>",
            "   <if test='status!=null and status!=\"\"'>and checkTime.status=#{status}</if>",
            "   <if test='result!=null and result!=\"\"'>and checkTime.result=#{result}</if>",
            "</where>",
            "</script>"
    })
    public List<CheckTime> findCheckByMap(Map<String,Object> map);

    /**
     *@Author: AndyZhang
     *@Description 找到某人某月什么状态下的考勤
     *@param
     *@return
     *@Date: 16:34 2021/2/20
     */
    @ResultMap("checkMap")
    @Select("select * from checkTime where year(date)=#{year} and month(date)=#{m} and tid=#{tid} and result=#{result}")
    public List<CheckTime> findCheckByMonth(String tid,String result,int m,int year);


    @ResultMap("checkMap")
    @Select("select checkTime.*,employee.name from checkTime,employee where checkTime.tid = employee.tellerId and checkTime.id=#{id}")
    public CheckTime findCheckById(String id);

    @Insert("insert into checkTime values(#{id},#{tid},#{date},#{startTime},#{endTime},#{status},#{result})")
    public void addCheck(CheckTime checkTime);

    @Update({
            "<script>",
            "update checkTime",
            "<trim prefix='set' suffixOverrides=','>",
            "<if test='tid!=null and tid!=\"\"'> tid=#{tid},</if>",
            "<if test='date!=null'>date=#{date},</if>",
            "<if test='month!=null'>month(date)=#{month},</if>",
            "<if test='year!=null'>year(date)=#{year},</if>",
            "<if test='startTime!=null'>startTime=#{startTime},</if>",
            "<if test='endTime!=null'>endTime=#{endTime},</if>",
            "<if test='status!=null and status!=\"\"'>status=#{status},</if>",
            "<if test='result!=null and result!=\"\"'>result=#{result},</if>",
            "</trim>",
            "where id = #{id}",
            "</script>"
    })
    public void updateCheck(CheckTime check);

//    @Update("update checkTime set status=#{status} where id = #{id}")
//    public void updateStatus(String id, String status);

    @Delete("delete from checkTime where id = #{id}")
    public void delCheck(String id);

    @Delete({
            "<script>",
            "delete from checkTime",
            "where id in",
            "<foreach item='item' collection='ids' open='(' close=')' separator=','>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public void delCheckByIds(List<String> ids);



}
